﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using agile_tool.userControl;

namespace agile_tool
{
    /// <summary>
    /// Interaction logic for projectPage.xaml
    /// </summary>
    public partial class projectPage : UserControl,IGenericPagesData
    {
        Button createNewProject;
        create createprojectItem;
        int userId;
        public string navigationName = "";
        public projectPage(int userId, string filter)
        {
            InitializeComponent();
            this.userId = userId;
            refresh(filter);
        }

        public string getNavigationName()
        {
            return navigationName;
        }
        public int getId()
        {
            return userId;
        }
        public void refresh(string filter)
        {
            lbProjects.Items.Clear();
            DataClasses1DataContext db = new DataClasses1DataContext();

            var projects = from p in db.tblProjects where p.userId == userId select p;
            //navigationName = projects.First().tblUser.userName;
            navigationName = "Projects";

            foreach (var p in projects)
            {
                if (filter == "")
                {
                    projectItem theprojectItem = new projectItem();
                    theprojectItem.tbName.Text = p.name;
                    theprojectItem.tbDesc.Text = p.description;
                    theprojectItem.id = p.pId;
                    theprojectItem.tbSprintDuration.Text = p.sprintDuration.ToString();
                    theprojectItem.btnDell.Click += new RoutedEventHandler(btnDell_Click);
                    theprojectItem.btnEdit.Click += new RoutedEventHandler(btnEdit_Click);
                    theprojectItem.btnUserStories.Click += new RoutedEventHandler(btnUserStories_Click);
                    theprojectItem.btnSprint.Click += new RoutedEventHandler(btnSprint_Click);
                    theprojectItem.btnResources.Click += new RoutedEventHandler(btnResources_Click);
                    theprojectItem.btnBurnDown.Click +=new RoutedEventHandler(btnBurnDown_Click);

                    lbProjects.Items.Add(theprojectItem);
                }
                else
                {
                    if (p.name.IndexOf(filter) != -1)
                    {
                        projectItem theprojectItem = new projectItem();
                        theprojectItem.tbName.Text = p.name;
                        theprojectItem.tbDesc.Text = p.description;
                        theprojectItem.id = p.pId;
                        theprojectItem.tbSprintDuration.Text = p.sprintDuration.ToString();
                        theprojectItem.btnDell.Click += new RoutedEventHandler(btnDell_Click);
                        theprojectItem.btnEdit.Click += new RoutedEventHandler(btnEdit_Click);
                        theprojectItem.btnUserStories.Click += new RoutedEventHandler(btnUserStories_Click);
                        theprojectItem.btnSprint.Click += new RoutedEventHandler(btnSprint_Click);
                        theprojectItem.btnResources.Click += new RoutedEventHandler(btnResources_Click);
                        theprojectItem.btnBurnDown.Click += new RoutedEventHandler(btnBurnDown_Click);

                        lbProjects.Items.Add(theprojectItem);
                    }
                }

            }
            createNewProject = new Button { Content = "Create New Project" };
            createNewProject.Click += new RoutedEventHandler(createNewProject_Click);
            createNewProject.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            lbProjects.Items.Add(createNewProject);
        }

        void btnResources_Click(object sender, RoutedEventArgs e)
        {
            //TODO
        }

        void btnSprint_Click(object sender, RoutedEventArgs e)
        {
            projectItem theprojectItem =new projectItem();
             theprojectItem = (projectItem)helper.FindParrentUserControl((Button)sender, theprojectItem.GetType().ToString());

             sprintPage theSprintPage = new sprintPage(theprojectItem.id, ((MainWindow)MainWindow.stateList[0]).tbSeatch.Text, theprojectItem.tbName.Text);
             MainWindow.stateList.Add(theSprintPage);
             ((MainWindow)MainWindow.stateList[0]).spBody.Children.Add(theSprintPage);
            ((MainWindow)MainWindow.stateList[0]).refreshNavButton();
            this.Visibility = Visibility.Collapsed;
        }
        
        
        Window w;
        void btnBurnDown_Click(object sender, RoutedEventArgs e)
        {
            lineChart lc = new lineChart();

            List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();
             projectItem theprojectItem =new projectItem();
             theprojectItem = (projectItem)helper.FindParrentUserControl((Button)sender, theprojectItem.GetType().ToString());


            DataClasses1DataContext db = new DataClasses1DataContext();
            int totalPriorityPoints = 0;
            var usstor = from p in db.tblUserStories where p.projectId == theprojectItem.id select p;
            foreach (var f in usstor)
            {
                totalPriorityPoints += (int)f.Priority;
            }

            var sprints = from p in db.tblSprints where p.projectId == theprojectItem.id select p;
            Dictionary<string, int> a = new Dictionary<string, int>();
            foreach (var f in sprints)
            {
                var userstories = from p in db.tblUserStorySprints where p.sprintId == f.id select p;
                foreach (var g in userstories)
                {
                        lc.addValue((int)g.tblUserStory.Priority, f.name);
                }
            }


            lc.createChartProject(totalPriorityPoints);
            w = new Window();
            w.Content = lc;
            w.Topmost = true;
            w.Show();
            lc.btnExit.Click += new RoutedEventHandler(btnExit_Click);
        }

        void btnExit_Click(object sender, RoutedEventArgs e)
        {
            w.Close();

        }
        void btnUserStories_Click(object sender, RoutedEventArgs e)
        {
             projectItem theprojectItem =new projectItem();
             theprojectItem = (projectItem)helper.FindParrentUserControl((Button)sender, theprojectItem.GetType().ToString());

             userStoryPage userStPage = new userStoryPage(theprojectItem.id, ((MainWindow)MainWindow.stateList[0]).tbSeatch.Text,theprojectItem.tbName.Text);
             MainWindow.stateList.Add(userStPage);
            ((MainWindow)MainWindow.stateList[0]).spBody.Children.Add(userStPage);
            ((MainWindow)MainWindow.stateList[0]).refreshNavButton();
            this.Visibility = Visibility.Collapsed;

        }

    
       
        void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            projectItem theprojectItem =new projectItem();
            theprojectItem = (projectItem)helper.FindParrentUserControl((Button)sender, theprojectItem.GetType().ToString());
            if (theprojectItem.spSlide.Height == 0)
            {
                theprojectItem.openTheDesc();
                theprojectItem.btnEdit.Content = "Save";
            }
            else
            {
                if (theprojectItem.tbName.Text == "")
                { MessageBox.Show("The Name Can't be Empty"); }
                else
                {
                    theprojectItem.closeTheDesc();
                    theprojectItem.btnEdit.Content = "Edit";
                    DataClasses1DataContext db = new DataClasses1DataContext();
                    tblProject tb = (from p in db.tblProjects where p.pId == theprojectItem.id select p).First();
                    tb.name = theprojectItem.tbName.Text;
                    tb.description = theprojectItem.tbDesc.Text;
                    tb.sprintDuration = int.Parse(theprojectItem.tbSprintDuration.Text);
                    db.SubmitChanges();
                }
            }
        }

        void btnDell_Click(object sender, RoutedEventArgs e)
        {
            projectItem theprojectItem = new projectItem();
            theprojectItem = (projectItem)helper.FindParrentUserControl(((Button)sender), theprojectItem.GetType().ToString());
            DataClasses1DataContext db = new DataClasses1DataContext();
            
            var tbus = from p in db.tblUserStories where p.projectId == theprojectItem.id select p;
            foreach (var u in tbus)
            {
                userStoryPage.deleteUserStory(u.id);
                db.tblUserStories.DeleteOnSubmit(u);
                db.SubmitChanges();
            }
           

            var tbsp = from p in db.tblSprints where p.projectId == theprojectItem.id select p;
            foreach (var u in tbsp)
            {
                sprintPage.deleteSprint(u.id);
                db.tblSprints.DeleteOnSubmit(u);
                db.SubmitChanges();
            }
            
            
            
            tblProject tb = (from p in db.tblProjects where p.pId == theprojectItem.id select p).First();
            db.tblProjects.DeleteOnSubmit(tb);
            db.SubmitChanges();

            lbProjects.Items.Remove(theprojectItem);
        }

        void createNewProject_Click(object sender, RoutedEventArgs e)
        {
            lbProjects.Items.Remove(createNewProject);
            createprojectItem = new create();
            createprojectItem.btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
            createprojectItem.btnSave.Click += new RoutedEventHandler(btnSave_Click);

            lbProjects.Items.Add(createprojectItem);
        }

        void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (createprojectItem.tbName.Text != "")
            {
                DataClasses1DataContext db = new DataClasses1DataContext();
                tblProject tp = new tblProject { name = createprojectItem.tbName.Text, description = createprojectItem.tbDesc.Text, userId = userId, sprintDuration = 0  };
                db.tblProjects.InsertOnSubmit(tp);
                db.SubmitChanges();

                refresh("");
            }
            else
            {
                MessageBox.Show("You need a name for your project");
            }
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {

            refresh("");
              
        }
    }
}
